﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Workflows.Designer
{
    public partial class ServiceInstaller : Form
    {
        public ServiceInstaller()
        {
            InitializeComponent();
        }

        private void ServiceInstaller_Load(object sender, EventArgs e)
        {
            this.txt_MachineName.Text = Environment.MachineName;
        }

        private void btn_RefreshServiceStatus_Click(object sender, EventArgs e)
        {
            string jobSvcName = ConfigurationManager.AppSettings.Get("JobDispatcherServiceName");
            string woxSvcName = ConfigurationManager.AppSettings.Get("WorkflowServiceName");
            bool installedJobDispatcher = false;
            bool startedJobDispatcher = false;
            bool installedWorkflowService = false;
            bool startedWorkflowService = false;
            ServiceController[] svcs = ServiceController.GetServices(this.txt_MachineName.Text);
            foreach(ServiceController svc in svcs)
            {
                if(svc.ServiceName==jobSvcName)
                {
                    installedJobDispatcher = true;
                    if(svc.Status==ServiceControllerStatus.Running)
                    {
                        startedJobDispatcher = true;
                    }
                }
                else if(svc.ServiceName==woxSvcName)
                {
                    installedWorkflowService = true;
                    if(svc.Status==ServiceControllerStatus.Running)
                    {
                        startedWorkflowService = true;
                    }
                }
            }
            this.SetJobDispatcherServiceStatus(installedJobDispatcher,startedJobDispatcher);
            this.SetWorkflowServiceStatus(installedWorkflowService,startedWorkflowService);
        }

        private void SetJobDispatcherServiceStatus(bool installed, bool started)
        {
            if (installed)
            {
                this.btn_InstallJobDispatcherService.Enabled = false;
                this.lbl_JobDispatcherStatus.ImageIndex = 5;
                this.lbl_JobDispatcherStatus.Text = "installed";

                if (started)
                {
                    this.btn_StartJobDispatcher.Enabled = false;
                    this.btn_StopJobDispatcher.Enabled = true;
                }
                else
                {
                    this.btn_StartJobDispatcher.Enabled = true;
                    this.btn_StopJobDispatcher.Enabled = false;
                }
            }
            else
            {
                this.btn_InstallJobDispatcherService.Enabled = true;
                this.lbl_JobDispatcherStatus.ImageIndex = 6;
                this.lbl_JobDispatcherStatus.Text = "Not installed";
                this.btn_StartJobDispatcher.Enabled = false;
                this.btn_StopJobDispatcher.Enabled = false;
            }
        }

        private void SetWorkflowServiceStatus(bool installed, bool started)
        {
            if(installed)
            {
                this.btn_InstallWorkflowService.Enabled = false;
                this.lbl_WorkflowServiceStatus.ImageIndex = 5;
                this.lbl_WorkflowServiceStatus.Text = "installed";
                if(started)
                {
                    this.btn_StartWorkflowService.Enabled = false;
                    this.btn_StopWorkflowService.Enabled = true;
                }
                else
                {
                    this.btn_StartWorkflowService.Enabled = true;
                    this.btn_StopWorkflowService.Enabled = false;
                }
            }
            else
            {
                this.btn_InstallWorkflowService.Enabled = true;
                this.lbl_WorkflowServiceStatus.ImageIndex = 6;
                this.lbl_WorkflowServiceStatus.Text = "Not installed";
                this.btn_StartWorkflowService.Enabled = false;
                this.btn_StopWorkflowService.Enabled = false;
            }
        }

        private void btn_InstallJobDispatcherService_Click(object sender, EventArgs e)
        {
            string setupFilePath = Path.Combine(Application.StartupPath, "services\\JobDispatcher\\setup.exe");
            Process proc=new Process();
            ProcessStartInfo startInfo=new ProcessStartInfo(setupFilePath);
            proc.StartInfo = startInfo;
            proc.Start();
            proc.WaitForExit();
            this.btn_RefreshServiceStatus_Click(sender,e);
        }

        private void btn_StartJobDispatcher_Click(object sender, EventArgs e)
        {
            string jobSvcName = ConfigurationManager.AppSettings.Get("JobDispatcherServiceName");
            ServiceController[] svcs = ServiceController.GetServices(this.txt_MachineName.Text);
            foreach (ServiceController svc in svcs)
            {
                if (svc.ServiceName == jobSvcName)
                {
                    svc.Start();
                    Thread.Sleep(5000);
                    this.btn_RefreshServiceStatus_Click(sender,e);
                    break;
                }
            }
        }

        private void btn_StopJobDispatcher_Click(object sender, EventArgs e)
        {
            string jobSvcName = ConfigurationManager.AppSettings.Get("JobDispatcherServiceName");
            ServiceController[] svcs = ServiceController.GetServices(this.txt_MachineName.Text);
            foreach (ServiceController svc in svcs)
            {
                if (svc.ServiceName == jobSvcName)
                {
                    svc.Stop();
                    Thread.Sleep(5000);
                    this.btn_RefreshServiceStatus_Click(sender, e);
                    break;
                }
            }
        }

        private void btn_InstallWorkflowService_Click(object sender, EventArgs e)
        {
            string setupFilePath = Path.Combine(Application.StartupPath, "services\\WorkflowService\\setup.exe");
            Process proc = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo(setupFilePath);
            proc.StartInfo = startInfo;
            proc.Start();
            proc.WaitForExit();
            this.btn_RefreshServiceStatus_Click(sender, e);
        }

        private void btn_StartWorkflowService_Click(object sender, EventArgs e)
        {
            string woxSvcName = ConfigurationManager.AppSettings.Get("WorkflowServiceName");
            ServiceController[] svcs = ServiceController.GetServices(this.txt_MachineName.Text);
            foreach (ServiceController svc in svcs)
            {
                if (svc.ServiceName == woxSvcName)
                {
                    svc.Start();
                    Thread.Sleep(5000);
                    this.btn_RefreshServiceStatus_Click(sender, e);
                    break;
                }
            }
        }

        private void btn_StopWorkflowService_Click(object sender, EventArgs e)
        {
            string woxSvcName = ConfigurationManager.AppSettings.Get("WorkflowServiceName");
            ServiceController[] svcs = ServiceController.GetServices(this.txt_MachineName.Text);
            foreach (ServiceController svc in svcs)
            {
                if (svc.ServiceName == woxSvcName)
                {
                    svc.Stop();
                    Thread.Sleep(5000);
                    this.btn_RefreshServiceStatus_Click(sender, e);
                    break;
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
